package cn.zwt.wolf.service.program;

import cn.zwt.wolf.domain.program.SpiderProgram;
import cn.zwt.wolf.repository.program.SpiderProgramRepository;
import cn.zwt.wolf.util.JPQLSearchException;
import cn.zwt.wolf.util.SearchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.NoResultException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangwt on 16-3-4.
 */
@Service
public class SpiderProgramService {
    @Autowired
    private SpiderProgramRepository spiderProgramRepository;
    @Autowired
    private SearchUtil searchUtil;
    private Logger logger = LoggerFactory.getLogger(SpiderProgramService.class);

    public List<SpiderProgram> findSpiderProgramsByStatus(int collectStatus){
        List<SpiderProgram> spiderPrograms = null;
        if ((Integer)collectStatus == null){
            return spiderPrograms;
        }
        String hql = "from SpiderProgram where collect_status=:collectStatus";
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("collectStatus", collectStatus);
        try {
            spiderPrograms = searchUtil.getResultList(hql,map);
        } catch (JPQLSearchException e) {
            logger.error("findSpiderProgramsByStatus error :" + e.getMessage());
        }catch (NoResultException e){
            logger.error("findSpiderProgramsByStatus error :" + e.getMessage());
        }
        return spiderPrograms;
    }

    public void savaOrUpdate(SpiderProgram spiderProgram){
        if (spiderProgram != null)
            spiderProgramRepository.save(spiderProgram);
    }

    public SpiderProgram findSpiderProgramById(int id){
        return spiderProgramRepository.findOne(id);
    }
}
